import { $get, $delete } from '@js/request'
import { WorkPerformanceSchema } from '@components/schema/performanceSchema'

interface DataSchema {
  create_datetime: number
  id: string
  key: string
  name: string
  operator: string
  operatorid: string
  pid: string
  sname: string
  status: number
  tenant_code: string
  type: string
  accounts: WorkPerformanceSchema[]
}

export interface AccountSchema {
  id: string | number
  realname: string
}

export interface TreeSchema {
  title: string
  key: string | number
  type: string
  children?: TreeSchema[]
  originData: any
}

// 获取树数据
export async function getTreeRequest (performanceid: string): Promise<TreeSchema[]> {
  const res = await $get('/api/commission/performance_work/list', {
    performanceid
  })
  return res.data.map((li: DataSchema) => formatTree(li))
}

function formatTree (data: DataSchema): TreeSchema {
  const r: TreeSchema = {
    title: data.name,
    key: data.id,
    type: 'department',
    originData: data
  }
  let list: TreeSchema[] = []
  if (data.accounts.length > 0) {
    const accounts = data.accounts.map(li => {
      return {
        title: li.name,
        key: li.id,
        type: 'account',
        originData: li
      }
    })
    list = accounts
  }
  if (list.length > 0) {
    r.children = list
  }
  return r
}

// 删除人员
export async function deleteRequest (performanceid: string, id: string): Promise<void> {
  const res = await $delete(`/api/commission/performance_work/${id}`)
  return res.data
}
